﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RegistrationController;

namespace HPMDesktop.Patient
{
    public partial class frmPatientSearch : Form
    {
        public frmPatientSearch()
        {
            InitializeComponent();
        }

        private void frmPatientSearch_Load(object sender, EventArgs e)
        {
            comboBoxSearchCriteria.Items.Add("CR Number");
            comboBoxSearchCriteria.Items.Add("Name");
            comboBoxSearchCriteria.Items.Add("Patient ID");
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxSearchCriteria_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxSearchCriteria.Text.Equals("CR Number")) {
                txtCRNum.Enabled = true;
                txtpFname.Enabled = false;
                txtpMname.Enabled = false;
                txtpLname.Enabled = false;
                txtrelFname.Enabled = false;
                txtrelMname.Enabled = false;
                txtrelLname.Enabled = false;
                txtPatientID.Enabled = false;
                cmdRegister.Enabled = false;
            }
            else if (comboBoxSearchCriteria.Text.Equals("Name"))
            {
                txtCRNum.Enabled = false;
                txtpFname.Enabled = true;
                txtpMname.Enabled = true;
                txtpLname.Enabled = true;
                txtrelFname.Enabled = true;
                txtrelMname.Enabled = true;
                txtrelLname.Enabled = true;
                txtPatientID.Enabled = false;
                cmdRegister.Enabled = true;
            }
            else if (comboBoxSearchCriteria.Text.Equals("Patient ID"))
            {
                txtCRNum.Enabled = false;
                txtpFname.Enabled = false;
                txtpMname.Enabled = false;
                txtpLname.Enabled = false;
                txtrelFname.Enabled = false;
                txtrelMname.Enabled = false;
                txtrelLname.Enabled = false;
                txtPatientID.Enabled = true;
                cmdRegister.Enabled = true;
            }
            else
            {
                txtCRNum.Enabled = false;
                txtpFname.Enabled = false;
                txtpMname.Enabled = false;
                txtpLname.Enabled = false;
                txtrelFname.Enabled = false;
                txtrelMname.Enabled = false;
                txtrelLname.Enabled = false;
                txtPatientID.Enabled = false;
                cmdRegister.Enabled = false;
            }
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            PatientRegistration_DataLoader objLoader = new PatientRegistration_DataLoader();
            PatientRegistration objPatient = new PatientRegistration();
            bool closeFrame = false;

            if (comboBoxSearchCriteria.Text.Equals("CR Number") &&
                txtCRNum.Text != "")
            {
                long tempPID = objLoader.Is_PatientCase_Open(null, txtCRNum.Text, objPatient);
                if (tempPID != -1)
                {
                    objPatient.PatientInfo.PatientID = tempPID.ToString();
                    objPatient.CRNumber = txtCRNum.Text;
                }
                else
                {
                    MessageBox.Show("Case for CR " + txtCRNum.Text + " does not exist.");
                }
            }
            else if (comboBoxSearchCriteria.Text.Equals("Name"))
            {
            }
            else if (comboBoxSearchCriteria.Text.Equals("Patient ID") &&
                     txtPatientID.Text != "")
            {
                long CRNumber = objLoader.Is_PatientCase_Open(txtPatientID.Text, null, objPatient);
                if (CRNumber != -1)
                {
                    objPatient.PatientInfo.PatientID = txtPatientID.Text;
                    objPatient.CRNumber = CRNumber.ToString();
                }
                else
                {
                    MessageBox.Show("Case for Patient ID " + txtPatientID.Text + " does not exist. Please register.");
                }
            }

            if (objPatient.PatientType != null &&
                objPatient.PatientType != "")
            {
                objPatient.PatientInfo = objLoader.Get_PATIENT_DATA(objPatient.PatientInfo.PatientID);
                frmPatientDetails frm = new frmPatientDetails(objPatient);
                frm.Show();
                closeFrame = true;
            }
            objLoader.Dispose();

            if (closeFrame)
            {
                this.Close();
            }
        }

        private void cmdRegister_Click(object sender, EventArgs e)
        {
            if (txtPatientID.Enabled &&
                txtPatientID.Text != "")
            {
                /* Check if the patient is already registered */
                PatientRegistration_DataLoader objLoader = new PatientRegistration_DataLoader();
                long CRNumber = objLoader.Is_PatientCase_Open(txtPatientID.Text, null, null);
                objLoader.Dispose();
                if (CRNumber > 0)
                {
                    MessageBox.Show("Case with CR " + CRNumber + " already open for PID " + txtPatientID.Text + ".");
                }
                else
                {
                    frmRegistration frm = new frmRegistration(txtPatientID.Text);
                    frm.Show();
                    this.Close();
                }
            }
        }
    }
}
